package servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.AppointmentDao;
import session.FootballMatchDao;
import session.PlayersDao;
import entity.FootballMatch;
import entity.User;

/**
 * Kontroler pravi listu igrača na koji se može glasati
 * i proverava da li korisnik ima prvao za glasanje
 *
 * @author rigo & jung
 * 
 */
public class PrepareVoteMvp extends HttpServlet {
	private static final long serialVersionUID = 1496232215674675323L;
	
	@EJB
	protected AppointmentDao appointmentDao;
	@EJB
	protected FootballMatchDao footballMatchDao;
	@EJB
	protected PlayersDao playersDao;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			User user = (User)request.getSession().getAttribute("user");
			if (user==null) {
				response.sendRedirect(response.encodeURL("./login.jsp"));
				return;
			}

			FootballMatch fo = footballMatchDao.getActive();
			
			request.setAttribute("players", playersDao.findAllByAppointment(fo.getAppointment()));
			request.setAttribute("currentPlayer", playersDao.findPlayerByAppointment(user, fo.getAppointment()));

			getServletContext().getRequestDispatcher("/voteForMvp.jsp").forward(request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
